package com.wontube.smr.remote.admin;

import java.util.List;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.cache.Cacher;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.dao.bo.BlogDao;
import com.wontube.smr.web.client.IBlogHandler;
import com.wontube.smr.web.client.admin.IAdminBlogHandler;
import com.wontube.smr.web.client.model.po.Blog;
import com.wontube.smr.web.client.model.po.BlogCategory;
import com.wontube.smr.web.client.model.po.BlogTag;
import com.wontube.smr.web.client.model.vo.BlogItems;

/**
 * @author tanggf(2012-5-25)
 *
 */
@NoLoginSupport
public class AdminBlogHandler  extends BasicHandler implements IAdminBlogHandler {

	@Override
	public BlogItems listBlogs(Integer status, String category, PagenateInfo pageInfo) {
		if(status != null && status.intValue() < 0){
			status = null;
		}
		return BlogDao.queryBlogs(status, category, pageInfo);
	}

	@Override
	public Blog getBlog(String id) {
		return BlogDao.getBlogByPk(id);
	}

	@Override
	public boolean addOrUpdateBlog(Blog blog) {
		boolean success = BlogDao.addOrUpdateBlog(blog);
		if(success){
			cleanBlogCaches();
		}
		return success;
	}

	@Override
	public boolean deleteBlog(String id) {	
		boolean success = BlogDao.updateBlogStatus(id, Blog.STATUS_DELETED);
		if(success){
			cleanBlogCaches();
		}
		return success;
	}

	@Override
	public boolean releaseBlog(String id) {
		boolean success = BlogDao.updateBlogStatus(id, Blog.STATUS_RELEASED);
		if(success){
			cleanBlogCaches();
		}
		return success;
	}		
	
	@Override
	public boolean cancleBlog(String id) {
		boolean success = BlogDao.updateBlogStatus(id, Blog.STATUS_DRAFT);
		if(success){
			cleanBlogCaches();
		}
		return success;
	}	
	
	@Override
	public boolean recallBlog(String id) {
		boolean success = BlogDao.updateBlogStatus(id, Blog.STATUS_DRAFT);
		if(success){
			cleanBlogCaches();
		}
		return success;
	}

	private void cleanBlogCaches(){
		String clazzName = IBlogHandler.class.getName();
		Cacher.defaultCacher.deleteAllCache(clazzName, "listBlogs", 1);
		Cacher.defaultCacher.deleteAllCache(clazzName, "getBlog", 1);
	}

	@Override
	public boolean addBlogCategory(BlogCategory cate) {
		boolean success = BlogDao.addBlogCategory(cate);
		if(success){
			cleanBlogCategoryCaches();
		}
		return success;
	}

	private void cleanBlogCategoryCaches() {
		// TODO 
	}

	@Override
	public boolean deleteBlogCategory(String id) {
		boolean success = BlogDao.deleteBlogCategory(id);
		if(success){
			cleanBlogCategoryCaches();
		}
		return success;
	}

	@Override
	public List<BlogCategory> listAllBlogCategories() {
		return BlogDao.listAllBlogCategories();
	}

	@Override
	public int addBlogTag(BlogTag tag) {
		int aff = BlogDao.addBlogTag(tag);
		if(aff == 1){
			cleanBlogTagCaches();
		}
		return aff;
	}

	private void cleanBlogTagCaches() {
		// TODO 
	}

	@Override
	public int deleteBlogTag(String id) {
		int aff = BlogDao.deleteBlogTag(id);
		if(aff == 1){
			cleanBlogTagCaches();
		}
		return aff;
	}

	@Override
	public List<BlogTag> listAllBlogTags() {
		return BlogDao.listAllBlogTags();
	}

	@Override
	public Object[] listAllBlogCategoryAndTags() {
		Object[] objs = new Object[2];
		objs[0] = listAllBlogCategories();
		objs[1] = listAllBlogTags();
		return objs;
	}
}
